/*
 * MIT License
 *
 * Copyright (c) 2023-present, tangli
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

package tony.test.core.utils

import com.fasterxml.jackson.core.type.TypeReference
import com.fasterxml.jackson.databind.type.TypeFactory
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Assertions.assertFalse
import org.junit.jupiter.api.Assertions.assertTrue
import org.junit.jupiter.api.DisplayName
import org.junit.jupiter.api.Nested
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.assertNotNull
import tony.core.utils.isArrayLikeType
import tony.core.utils.isArrayType
import tony.core.utils.isBooleanType
import tony.core.utils.isByteType
import tony.core.utils.isDateTimeLikeType
import tony.core.utils.isDoubleType
import tony.core.utils.isFloatType
import tony.core.utils.isIntType
import tony.core.utils.isLongType
import tony.core.utils.isNumberTypes
import tony.core.utils.isObjLikeType
import tony.core.utils.isShortType
import tony.core.utils.isSimpleType
import tony.core.utils.isStringLikeType
import tony.core.utils.isTypesOrSubTypesOf
import tony.core.utils.isVoidLikeType
import tony.core.utils.rawClass
import tony.core.utils.toCollectionJavaType
import tony.core.utils.toJavaType
import tony.core.utils.typeParamOfSuperClass
import java.lang.reflect.ParameterizedType
import java.lang.reflect.Type
import java.math.BigDecimal
import java.math.BigInteger
import java.time.LocalDateTime
import java.util.Date
import java.util.LinkedList

/**
 * 类型工具类单元测试
 * @author tangli
 * @date 2025/08/05 14:00
 */
@DisplayName("Types测试")
class TypesTest {

    // 测试用的泛型类
    abstract class TestGenericClass<T> {
        public fun getType(): Type = (javaClass.genericSuperclass as ParameterizedType).actualTypeArguments[0]
    }

    class StringGenericClass : TestGenericClass<String>()

    @Nested
    @DisplayName("Type扩展方法测试")
    inner class TypeExtensionTest {
        @Test
        @DisplayName("Type.rawClass():Class类型")
        public fun testRawClassWithClass() {
            val stringClass: Type = String::class.java
            val result = stringClass.rawClass()
            assertEquals(String::class.java, result)
        }

        @Test
        @DisplayName("Type.rawClass():ParameterizedType类型")
        public fun testRawClassWithParameterizedType() {
            val genericClass = StringGenericClass()
            val type = genericClass.getType()
            val result = type.rawClass()
            assertEquals(String::class.java, result)
        }

        @Test
        @DisplayName("Type.toJavaType():转换JavaType")
        public fun testToJavaType() {
            val stringClass: Type = String::class.java
            val javaType = stringClass.toJavaType()
            assertNotNull(javaType)
            assertEquals(String::class.java, javaType.rawClass)
        }

        @Test
        @DisplayName("Type.toCollectionJavaType():集合类型转换")
        public fun testToCollectionJavaType() {
            val stringClass: Type = String::class.java
            val collectionType = stringClass.toCollectionJavaType(List::class.java)
            assertNotNull(collectionType)
            assertTrue(collectionType.isCollectionLikeType)
        }

        @Test
        @DisplayName("Type.isTypesOrSubTypesOf():变参形式")
        public fun testIsTypesOrSubTypesOfVararg() {
            val targetType = ArrayList::class.java
            val result = targetType.isTypesOrSubTypesOf(List::class.java, Collection::class.java, Object::class.java)
            assertTrue(result)
        }

        @Test
        @DisplayName("Type.isTypesOrSubTypesOf():集合形式")
        public fun testIsTypesOrSubTypesOfCollection() {
            val targetType = ArrayList::class.java
            val types = listOf(List::class.java, Collection::class.java)
            val result = targetType.isTypesOrSubTypesOf(types)
            assertTrue(result)
        }

        @Test
        @DisplayName("Type.isTypesOrSubTypesOf():ParameterizedType类型")
        public fun testIsTypesOrSubTypesOfParameterizedType() {
            val genericClass = StringGenericClass()
            val type = genericClass.getType()
            val result = type.isTypesOrSubTypesOf(String::class.java, CharSequence::class.java)
            assertTrue(result)
        }

        @Test
        @DisplayName("Type.isTypesOrSubTypesOf():不支持的类型")
        public fun testIsTypesOrSubTypesOfUnsupportedType() {
            // 创建一个不支持的类型（这里使用一个模拟的Type实现）
            val unsupportedType = object : Type {
                override fun getTypeName(): String = "UnsupportedType"
            }
            val result = unsupportedType.isTypesOrSubTypesOf(String::class.java)
            assertFalse(result)
        }

        @Test
        @DisplayName("Type.isNumberTypes():数字类型")
        public fun testIsNumberTypes() {
            val numberTypes = listOf(
                Long::class.java,
                Int::class.java,
                Double::class.java,
                Float::class.java,
                Byte::class.java,
                Short::class.java,
                BigInteger::class.java,
                BigDecimal::class.java
            )
            numberTypes.forEach { type ->
                assertTrue(type.isNumberTypes())
            }
        }

        @Test
        @DisplayName("Type.isNumberTypes():非数字类型")
        public fun testIsNumberTypesNonNumber() {
            val nonNumberTypes = listOf(
                String::class.java,
                Boolean::class.java,
                Date::class.java,
                List::class.java
            )
            nonNumberTypes.forEach { type ->
                assertFalse(type.isNumberTypes())
            }
        }

        @Test
        @DisplayName("Type.isBooleanType():布尔类型")
        public fun testIsBooleanType() {
            assertTrue(Boolean::class.java.isBooleanType())
            assertTrue(Boolean::class.javaPrimitiveType!!.isBooleanType())
        }

        @Test
        @DisplayName("Type.isBooleanType():非布尔类型")
        public fun testIsBooleanTypeNonBoolean() {
            assertFalse(String::class.java.isBooleanType())
            assertFalse(Int::class.java.isBooleanType())
        }

        @Test
        @DisplayName("Type.isArrayType():数组类型")
        public fun testIsArrayType() {
            assertTrue(Array<String>::class.java.isArrayType())
            assertTrue(IntArray::class.java.isArrayType())
        }

        @Test
        @DisplayName("Type.isArrayType():非数组类型")
        public fun testIsArrayTypeNonArray() {
            assertFalse(String::class.java.isArrayType())
            assertFalse(List::class.java.isArrayType())
        }

        @Test
        @DisplayName("Type.isArrayLikeType():数组类型")
        public fun testIsArrayLikeType() {
            val arrayTypes = listOf(
                Array<String>::class.java,
                List::class.java,
                Set::class.java,
                Collection::class.java,
                ArrayList::class.java,
                LinkedList::class.java
            )
            arrayTypes.forEach { type ->
                assertTrue(type.isArrayLikeType())
            }
        }

        @Test
        @DisplayName("Type.isArrayLikeType():非数组类型")
        public fun testIsArrayLikeTypeNonArray() {
            val nonArrayTypes = listOf(
                String::class.java,
                Int::class.java,
                Boolean::class.java,
                Date::class.java
            )
            nonArrayTypes.forEach { type ->
                assertFalse(type.isArrayLikeType())
            }
        }

        @Test
        @DisplayName("Type.isStringLikeType():字符串类型")
        public fun testIsStringLikeType() {
            val stringTypes = listOf(
                String::class.java,
                CharSequence::class.java,
                StringBuilder::class.java,
                StringBuffer::class.java
            )
            stringTypes.forEach { type ->
                assertTrue(type.isStringLikeType())
            }
        }

        @Test
        @DisplayName("Type.isStringLikeType():非字符串类型")
        public fun testIsStringLikeTypeNonString() {
            val nonStringTypes = listOf(
                Int::class.java,
                Boolean::class.java,
                Date::class.java,
                List::class.java
            )
            nonStringTypes.forEach { type ->
                assertFalse(type.isStringLikeType())
            }
        }

        @Test
        @DisplayName("Type.isDateTimeLikeType():日期时间类型")
        public fun testIsDateTimeLikeType() {
            assertTrue(Date::class.java.isDateTimeLikeType())
            assertTrue(LocalDateTime::class.java.isDateTimeLikeType())
        }

        @Test
        @DisplayName("Type.isDateTimeLikeType():非日期时间类型")
        public fun testIsDateTimeLikeTypeNonDateTime() {
            assertFalse(String::class.java.isDateTimeLikeType())
            assertFalse(Int::class.java.isDateTimeLikeType())
        }

        @Test
        @DisplayName("Type.isSimpleType():简单类型")
        public fun testIsSimpleType() {
            // 字符串类型
            assertTrue(String::class.java.isSimpleType())
            // 数字类型
            assertTrue(Int::class.java.isSimpleType())
            assertTrue(Long::class.java.isSimpleType())
            // 布尔类型
            assertTrue(Boolean::class.java.isSimpleType())
            // 日期时间类型
            assertTrue(Date::class.java.isSimpleType())
            // 枚举类型
            assertTrue(TestEnum::class.java.isSimpleType())
        }

        @Test
        @DisplayName("Type.isSimpleType():非简单类型")
        public fun testIsSimpleTypeNonSimple() {
            assertFalse(List::class.java.isSimpleType())
            assertFalse(Map::class.java.isSimpleType())
            assertFalse(TestUser::class.java.isSimpleType())
        }

        @Test
        @DisplayName("Type.isVoidLikeType():Void类型")
        public fun testIsVoidLikeType() {
            assertTrue(Void.TYPE.isVoidLikeType())
            assertTrue(Unit::class.java.isVoidLikeType())
        }

        @Test
        @DisplayName("Type.isVoidLikeType():非Void类型")
        public fun testIsVoidLikeTypeNonVoid() {
            assertFalse(String::class.java.isVoidLikeType())
            assertFalse(Int::class.java.isVoidLikeType())
        }
    }

    @Nested
    @DisplayName("Class扩展方法测试")
    inner class ClassExtensionTest {
        @Test
        @DisplayName("Class.typeParamOfSuperClass():获取父类泛型参数")
        public fun testTypeParamOfSuperClass() {
            val genericClass = StringGenericClass()
            val type = genericClass.javaClass.typeParamOfSuperClass()
            assertEquals(String::class.java, type)
        }

        @Test
        @DisplayName("Class.typeParamOfSuperClass():指定索引")
        public fun testTypeParamOfSuperClassWithIndex() {
            val genericClass = StringGenericClass()
            val type = genericClass.javaClass.typeParamOfSuperClass(0)
            assertEquals(String::class.java, type)
        }
    }

    @Nested
    @DisplayName("TypeReference扩展方法测试")
    inner class TypeReferenceExtensionTest {
        @Test
        @DisplayName("TypeReference.rawClass():获取原始类")
        public fun testTypeReferenceRawClass() {
            val typeRef = object : TypeReference<String>() {}
            val result = typeRef.rawClass()
            assertEquals(String::class.java, result)
        }

        @Test
        @DisplayName("TypeReference.isStringLikeType():字符串类型")
        public fun testTypeReferenceIsStringLikeType() {
            val stringTypeRef = object : TypeReference<String>() {}
            assertTrue(stringTypeRef.isStringLikeType())

            val charTypeRef = object : TypeReference<Char>() {}
            assertTrue(charTypeRef.isStringLikeType())
        }

        @Test
        @DisplayName("TypeReference.isStringLikeType():非字符串类型")
        public fun testTypeReferenceIsStringLikeTypeNonString() {
            val intTypeRef = object : TypeReference<Int>() {}
            assertFalse(intTypeRef.isStringLikeType())
        }

        @Test
        @DisplayName("TypeReference.isNumberTypes():数字类型")
        public fun testTypeReferenceIsNumberTypes() {
            val intTypeRef = object : TypeReference<Int>() {}
            assertTrue(intTypeRef.isNumberTypes())

            val longTypeRef = object : TypeReference<Long>() {}
            assertTrue(longTypeRef.isNumberTypes())

            val bigIntegerTypeRef = object : TypeReference<BigInteger>() {}
            assertTrue(bigIntegerTypeRef.isNumberTypes())
        }

        @Test
        @DisplayName("TypeReference.isNumberTypes():非数字类型")
        public fun testTypeReferenceIsNumberTypesNonNumber() {
            val stringTypeRef = object : TypeReference<String>() {}
            assertFalse(stringTypeRef.isNumberTypes())
        }
    }

    @Nested
    @DisplayName("JavaType扩展方法测试")
    inner class JavaTypeExtensionTest {
        @Test
        @DisplayName("JavaType.rawClass():获取原始类")
        public fun testJavaTypeRawClass() {
            val javaType = TypeFactory.defaultInstance().constructType(String::class.java)
            val result = javaType.rawClass<String>()
            assertEquals(String::class.java, result)
        }

        @Test
        @DisplayName("JavaType.toCollectionJavaType():转换为集合类型")
        public fun testJavaTypeToCollectionJavaType() {
            val javaType = TypeFactory.defaultInstance().constructType(String::class.java)
            val collectionType = javaType.toCollectionJavaType(List::class.java)
            assertNotNull(collectionType)
            assertTrue(collectionType.isCollectionLikeType)
        }

        @Test
        @DisplayName("JavaType.isNumberTypes():数字类型")
        public fun testJavaTypeIsNumberType() {
            val intType = TypeFactory.defaultInstance().constructType(Int::class.java)
            assertTrue(intType.isNumberTypes())

            val longType = TypeFactory.defaultInstance().constructType(Long::class.java)
            assertTrue(longType.isNumberTypes())

            val bigDecimalType = TypeFactory.defaultInstance().constructType(BigDecimal::class.java)
            assertTrue(bigDecimalType.isNumberTypes())

            val bigIntegerType = TypeFactory.defaultInstance().constructType(BigInteger::class.java)
            assertTrue(bigIntegerType.isNumberTypes())
        }

        @Test
        @DisplayName("JavaType.isNumberTypes():非数字类型")
        public fun testJavaTypeIsNumberTypeNonNumber() {
            val stringType = TypeFactory.defaultInstance().constructType(String::class.java)
            assertFalse(stringType.isNumberTypes())
        }

        @Test
        @DisplayName("JavaType.isBooleanType():布尔类型")
        public fun testJavaTypeIsBooleanType() {
            val booleanType = TypeFactory.defaultInstance().constructType(Boolean::class.java)
            assertTrue(booleanType.isBooleanType())
        }

        @Test
        @DisplayName("JavaType.isBooleanType():非布尔类型")
        public fun testJavaTypeIsBooleanTypeNonBoolean() {
            val stringType = TypeFactory.defaultInstance().constructType(String::class.java)
            assertFalse(stringType.isBooleanType())
        }

        @Test
        @DisplayName("JavaType.isByteType():字节类型")
        public fun testJavaTypeIsByteType() {
            val byteType = TypeFactory.defaultInstance().constructType(Byte::class.java)
            assertTrue(byteType.isByteType())
        }

        @Test
        @DisplayName("JavaType.isShortType():短整型")
        public fun testJavaTypeIsShortType() {
            val shortType = TypeFactory.defaultInstance().constructType(Short::class.java)
            assertTrue(shortType.isShortType())
        }

        @Test
        @DisplayName("JavaType.isIntType():整型")
        public fun testJavaTypeIsIntType() {
            val intType = TypeFactory.defaultInstance().constructType(Int::class.java)
            assertTrue(intType.isIntType())
        }

        @Test
        @DisplayName("JavaType.isLongType():长整型")
        public fun testJavaTypeIsLongType() {
            val longType = TypeFactory.defaultInstance().constructType(Long::class.java)
            assertTrue(longType.isLongType())
        }

        @Test
        @DisplayName("JavaType.isFloatType():浮点型")
        public fun testJavaTypeIsFloatType() {
            val floatType = TypeFactory.defaultInstance().constructType(Float::class.java)
            assertTrue(floatType.isFloatType())
        }

        @Test
        @DisplayName("JavaType.isDoubleType():双精度浮点型")
        public fun testJavaTypeIsDoubleType() {
            val doubleType = TypeFactory.defaultInstance().constructType(Double::class.java)
            assertTrue(doubleType.isDoubleType())
        }

        @Test
        @DisplayName("JavaType.isDateTimeLikeType():日期时间类型")
        public fun testJavaTypeIsDateTimeLikeType() {
            val dateType = TypeFactory.defaultInstance().constructType(Date::class.java)
            assertTrue(dateType.isDateTimeLikeType())

            val localDateTimeType = TypeFactory.defaultInstance().constructType(LocalDateTime::class.java)
            assertTrue(localDateTimeType.isDateTimeLikeType())
        }

        @Test
        @DisplayName("JavaType.isDateTimeLikeType():非日期时间类型")
        public fun testJavaTypeIsDateTimeLikeTypeNonDateTime() {
            val stringType = TypeFactory.defaultInstance().constructType(String::class.java)
            assertFalse(stringType.isDateTimeLikeType())
        }

        @Test
        @DisplayName("JavaType.isStringLikeType():字符串类型")
        public fun testJavaTypeIsStringLikeType() {
            val stringType = TypeFactory.defaultInstance().constructType(String::class.java)
            assertTrue(stringType.isStringLikeType())

            val charType = TypeFactory.defaultInstance().constructType(Char::class.java)
            assertTrue(charType.isStringLikeType())
        }

        @Test
        @DisplayName("JavaType.isStringLikeType():非字符串类型")
        public fun testJavaTypeIsStringLikeTypeNonString() {
            val intType = TypeFactory.defaultInstance().constructType(Int::class.java)
            assertFalse(intType.isStringLikeType())
        }

        @Test
        @DisplayName("JavaType.isArrayLikeType():数组类型")
        public fun testJavaTypeIsArrayLikeType() {
            val arrayType = TypeFactory.defaultInstance().constructArrayType(String::class.java)
            assertTrue(arrayType.isArrayLikeType())

            val collectionType = TypeFactory.defaultInstance().constructCollectionType(List::class.java, String::class.java)
            assertTrue(collectionType.isArrayLikeType())
        }

        @Test
        @DisplayName("JavaType.isArrayLikeType():非数组类型")
        public fun testJavaTypeIsArrayLikeTypeNonArray() {
            val stringType = TypeFactory.defaultInstance().constructType(String::class.java)
            assertFalse(stringType.isArrayLikeType())
        }

        @Test
        @DisplayName("JavaType.isObjLikeType():对象类型")
        public fun testJavaTypeIsObjLikeType() {
            val mapType = TypeFactory.defaultInstance().constructMapType(Map::class.java, String::class.java, String::class.java)
            assertTrue(mapType.isObjLikeType())

            val customType = TypeFactory.defaultInstance().constructType(TestUser::class.java)
            assertTrue(customType.isObjLikeType())
        }

        @Test
        @DisplayName("JavaType.isObjLikeType():非对象类型")
        public fun testJavaTypeIsObjLikeTypeNonObject() {
            val stringType = TypeFactory.defaultInstance().constructType(String::class.java)
            assertFalse(stringType.isObjLikeType())

            val intType = TypeFactory.defaultInstance().constructType(Int::class.java)
            assertFalse(intType.isObjLikeType())
        }
    }

    // 测试用的数据类
    public data class TestUser(public val id: Int, public val name: String)

    // 测试用的枚举
    public enum class TestEnum {
        VALUE1, VALUE2
    }
}
